package com.yaodian100.ec.payment.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.core.common.service.DomainService;
import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.ec.common.EcConstant;
import com.yaodian100.ec.payment.entity.ThirdPartyTransfer;
import com.yaodian100.ec.payment.service.TransferService;

/**
 * @author jinwei.lin
 * 
 */
public class ThirdPartyPaymentAction extends ActionSupport implements Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 548308224134647780L;

	/** logger */
	private final Logger logger = LoggerFactory.getLogger(ThirdPartyPaymentAction.class);

	// private Menu statusMenu; // 與 getter, setter
	private SimplePager pager;
	private DomainService<Menu> menuService; // 與 setter
	private TransferService transferService;

	// 查詢條件
	private String idParam;
	private String tradeStartDtStParam;
	private String tradeStartDtEdParam;
	private String orderIdParam;
	private String minDate;
	private String today;
	private String orderId;
	private Menu srcMenu;
	private String tppId;

	private ThirdPartyTransfer thirdParty;
	private Collection<ThirdPartyTransfer> thirdPartyList;

	/**
	 * 檢視汇款资料
	 * 
	 * @return
	 */
	public String view() throws Exception {
		if (StringUtils.isNotBlank(tppId) || thirdParty.getId() != null) {
			try {
				if (null != tppId)
					thirdParty = transferService.get(tppId);
				else
					thirdParty = transferService.get(thirdParty.getId());
				if (srcMenu == null) {
					srcMenu = menuService.get("Payment.Src");
				}
			} catch (CoreException e) {
				addActionError(e.getMessage());
				e.printStackTrace();
			}
		}
		return "view";
	}

	/**
	 * 汇款資料列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String query() throws Exception {
		CommonCriteria crit = new CommonCriteria();

		if (StringUtils.isBlank(tradeStartDtStParam))
			tradeStartDtStParam = EcConstant.PATTERN_DATE_FORMAT.format(new Date());
		if (StringUtils.isBlank(tradeStartDtEdParam))
			tradeStartDtEdParam = EcConstant.PATTERN_DATE_FORMAT.format(new Date());

		DateFormat format = new SimpleDateFormat(EcConstant.PATTERN_TIMESTAMP);

		crit.addGe("tradeStartDt", format.parse(tradeStartDtStParam + " 00:00:00"));
		crit.addLe("tradeStartDt", format.parse(tradeStartDtEdParam + " 23:59:59"));

		if (StringUtils.isNotBlank(idParam))
			crit.addRlike("id", idParam);
		if (StringUtils.isNotBlank(orderIdParam))
			crit.addEq("orderId", orderIdParam);

		if (srcMenu == null) {
			srcMenu = menuService.get("Payment.Src");
		}

		if (pager == null)
			pager = new SimplePager();
		int entitySize = transferService.getListSize(crit).intValue();
		pager.setTotalSize(entitySize);

		thirdPartyList = transferService.getList(pager.getCurrentPage() * pager.getPageRecord(), pager.getPageRecord(),
				crit, null);
		logger.info("pager:{}", pager);

		return "list";
	}

	public String queryByOrder() throws Exception {
		CommonCriteria crit = new CommonCriteria();
		crit.addEq("orderId", orderId);

		if (srcMenu == null) {
			srcMenu = menuService.get("Payment.Src");
		}

		thirdPartyList = transferService.getList(0, -1, crit, null);

		return "listByOrder";
	}

	/**
	 * 查询
	 * 
	 * @return
	 * @throws Exception
	 */
	public String search() throws Exception {
		return "search";
	}

	public void prepare() throws Exception {
		clearErrorsAndMessages();
		if (org.apache.commons.lang.StringUtils.isBlank(tradeStartDtStParam))
			tradeStartDtStParam = DateUtil.convertDateToString("yyyy-MM-dd", DateUtil.getToday().getTime());
		if (org.apache.commons.lang.StringUtils.isBlank(tradeStartDtEdParam))
			tradeStartDtEdParam = DateUtil.convertDateToString("yyyy-MM-dd", DateUtil.getToday().getTime());

		if (StringUtils.isBlank(minDate))
			minDate = DateUtil.convertDateToString("yyyy,MM - 1,dd", DateUtil.getTodayAddDays(-50).getTime());
		if (StringUtils.isBlank(today))
			today = DateUtil.convertDateToString("yyyy,MM - 1,dd", DateUtil.getToday().getTime());

		// if (statusMenu == null) {
		// statusMenu = menuService.get("bankTransfer.status");
		// }

	}

	public SimplePager getPager() {
		return pager;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public String getIdParam() {
		return idParam;
	}

	public void setIdParam(String idParam) {
		this.idParam = idParam;
	}

	public String getMinDate() {
		return minDate;
	}

	public void setMinDate(String minDate) {
		this.minDate = minDate;
	}

	public String getToday() {
		return today;
	}

	public void setToday(String today) {
		this.today = today;
	}

	public String getTppId() {
		return tppId;
	}

	public void setTppId(String tppId) {
		this.tppId = tppId;
	}

	public void setMenuService(DomainService<Menu> menuService) {
		this.menuService = menuService;
	}

	public void setTransferService(TransferService transferService) {
		this.transferService = transferService;
	}

	public String getTradeStartDtEdParam() {
		return tradeStartDtEdParam;
	}

	public void setTradeStartDtEdParam(String tradeStartDtEdParam) {
		this.tradeStartDtEdParam = tradeStartDtEdParam;
	}

	public String getOrderIdParam() {
		return orderIdParam;
	}

	public void setOrderIdParam(String orderIdParam) {
		this.orderIdParam = orderIdParam;
	}

	public ThirdPartyTransfer getThirdParty() {
		return thirdParty;
	}

	public void setThirdParty(ThirdPartyTransfer thirdParty) {
		this.thirdParty = thirdParty;
	}

	public Collection<ThirdPartyTransfer> getThirdPartyList() {
		return thirdPartyList;
	}

	public void setThirdPartyList(Collection<ThirdPartyTransfer> thirdPartyList) {
		this.thirdPartyList = thirdPartyList;
	}

	public String getTradeStartDtStParam() {
		return tradeStartDtStParam;
	}

	public void setTradeStartDtStParam(String tradeStartDtStParam) {
		this.tradeStartDtStParam = tradeStartDtStParam;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public Menu getSrcMenu() {
		return srcMenu;
	}

	public void setSrcMenu(Menu srcMenu) {
		this.srcMenu = srcMenu;
	}
}